package niord.apps.database;

import java.util.ArrayList;

import niord.apps.Order;
import niord.apps.Person;
import niord.apps.Place;
import niord.apps.Product;
import android.content.Context;
import android.database.Cursor;

public class DataBase {

	private DataBaseAdapter dbAdapter;

	public static ArrayList<Person> persons = new ArrayList<Person>();
	public static ArrayList<Place> places = new ArrayList<Place>();
	public static ArrayList<Product> products = new ArrayList<Product>();
	public static ArrayList<Order> orders = new ArrayList<Order>();

	// CTOR
	public DataBase(Context context) {
		dbAdapter = new DataBaseAdapter(context);
		dbAdapter.open();
	}
	/***********************************************************************/
	public void saveData(ArrayList<Product> persons) {
		// Clear all tables in database
		dbAdapter.deleteAll();

		/******* Add new data *******/
		// Persons
		for (Person person : DataBase.persons) {
			dbAdapter.createPerson(person.getName());
		}

		// Places
		for (Place place : places) {
			dbAdapter.createPlace(place.getName(), place.getAddress(),
					place.getPhone());
		}

		// Products
		for (Product product : products) {
			dbAdapter.createProduct(product.getName(), product.getPrice(),
					product.getTypeId(), product.getUomId(), product.getPlaceId());
		}

		// Orders
		for (Order order : orders) {
			dbAdapter.createOrder(order.getPersonId(), order.getProductId(),
					order.getQuantity());
		}
	}
	/***********************************************************************/
	public void restorePersons() {

		persons.clear();

		// Obtain cursor with persons data
		Cursor personEntries = dbAdapter.fetchAllPersons();

		// Parse data
		if (personEntries.moveToFirst()) {
			do {
				Long id = personEntries.getLong(personEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_ID));
				String name = personEntries.getString(personEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_NAME));

				persons.add(new Person(id, name));

			}
			while (personEntries.moveToNext());
		}

		personEntries.close();
	}
	/***********************************************************************/
	public void restorePlaces() {

		places.clear();

		// Obtain cursor with places data
		Cursor placeEntries = dbAdapter.fetchAllPlaces();

		// Parse data
		if (placeEntries.moveToFirst()) {
			do {
				Long id = placeEntries.getLong(placeEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_ID));
				String name = placeEntries.getString(placeEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_NAME));
				String address = placeEntries.getString(placeEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_ADDR));
				String phone = placeEntries.getString(placeEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_PHN));

				places.add(new Place(id, name, address, phone));

			}
			while (placeEntries.moveToNext());
		}

		placeEntries.close();

	}
	/***********************************************************************/
	public void restoreProducts() {

		products.clear();

		// Obtain cursor with persons data
		Cursor productEntries = dbAdapter.fetchAllProducts();

		// Parse data
		if (productEntries.moveToFirst()) {
			do {
				Long id = productEntries.getLong(productEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_ID));
				String name = productEntries.getString(productEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_NAME));
				Double price = productEntries.getDouble(productEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_PRICE));
				Long type_id = productEntries.getLong(productEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_TYPE_ID));
				Long uom_id = productEntries.getLong(productEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_UOM_ID));
				Long place_id = productEntries.getLong(productEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_PLC_ID));

				products
						.add(new Product(id, name, price, type_id, uom_id, place_id));

			}
			while (productEntries.moveToNext());
		}

		productEntries.close();

	}
	/***********************************************************************/
	public void restoreOrders() {

		orders.clear();

		// Obtain cursor with persons data
		Cursor orderEntries = dbAdapter.fetchAllOrders();

		// Parse data
		if (orderEntries.moveToFirst()) {
			do {
				Long id = orderEntries.getLong(orderEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_ID));
				Long person_id = orderEntries.getLong(orderEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_PSN_ID));
				Long product_id = orderEntries.getLong(orderEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_PRD_ID));
				Long quantity = orderEntries.getLong(orderEntries
						.getColumnIndexOrThrow(DataBaseAdapter.KEY_QUAN));

				orders.add(new Order(id, person_id, product_id, quantity));

			}
			while (orderEntries.moveToNext());
		}

		orderEntries.close();

	}
	/***********************************************************************/
	public void restoreData() {
		restorePersons();
		restorePlaces();
		restoreProducts();
		restoreOrders();
	}
	/***********************************************************************/
	// Delete all data
	public void deleteData() {
		dbAdapter.deleteAll();
	}
	/***********************************************************************/
	// Close connection to database
	public void close() {
		if (dbAdapter != null) {
			dbAdapter.close();
		}
	}
}
